import 'package:chathouse/pages/questions/extent.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/widgets/form_widgets/select_item_widget16.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../model/pre_question_bean.dart';
import '../../widgets/form_widgets/area_select_widget7.dart';
import '../../widgets/form_widgets/date_pick_widget8.dart';
import '../../widgets/form_widgets/drpo_down_widget3.dart';
import '../../widgets/form_widgets/image_picker_widget11.dart';
import '../../widgets/form_widgets/input_widget4.dart';
import '../../widgets/form_widgets/location_select_widget9.dart';
import '../../widgets/form_widgets/multiple_select_and_input_widget6.dart';
import '../../widgets/form_widgets/multiple_select_widget2.dart';
import '../../widgets/form_widgets/single_select_and_input_widget12.dart';
import '../../widgets/form_widgets/single_select_and_input_widget5.dart';
import '../../widgets/form_widgets/single_select_widget1.dart';
import '../../widgets/form_widgets/time_pick_widget10.dart';

//14种问题
List<Widget> getCommandSubWidget(
    PreQuestionBeanTool? preQuestionBeanTool,
    serviceId,
    Function setNextButtonCanClick,
    paras,
    p6,
    p9,
    Map<int, dynamic> tempMap,
    {int? page}) {
  List<Widget> subWidget = [];
  if (preQuestionBeanTool?.isRequestSuccess ?? false) {
    preQuestionBeanTool?.rows?.forEach((element) {
      //配置下一次参数
      int index = preQuestionBeanTool?.rows?.indexOf(element) ?? 0;
      Map<String, dynamic> para = {};
      if (index < paras.length) {
        para = paras[index];
      } else {
        para = {
          "problemId": element.problemId,
          // "pagesNum": page ?? 1,
          "pagesNum": element.pagesNum,
          "serviceId": preQuestionBeanTool.serviceId ?? serviceId,
          "firstProblemId": element.problemId,
          "required": element.required,
        };
        paras.add(para);
      }
      bool request = element.required == "0";
      //widget
      String title = element.problemName ?? "";
      int problemId = element.problemId ?? 0;
      Widget widgets = Container();
      switch (int.parse(element.problemType ?? "0")) {
        case 1:
          widgets = SingleSelectWidget1(
            title,
            tempMap[problemId],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val ?? "";
              para["optionValue"] = getOptionValueByOptionCode(
                  preQuestionBeanTool,
                  element.problemId,
                  para["firstOptionCode"]);
              setNextButtonCanClick();
            },
            type: 1,
            request: request,
          );
          break;
        case 2:
          widgets = MultipleSelectWidget2(
            title,
            tempMap[problemId] ?? [],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val.toParaString();
              para["optionValue"] = val
                  .map((e) {
                    return getOptionValueByOptionCode(
                            preQuestionBeanTool, element.problemId, e) ??
                        "";
                  })
                  .toList()
                  .toParaString();
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 3:
          widgets = DropDownWidget3(
            title,
            tempMap[problemId],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val;
              para["optionValue"] = getOptionValueByOptionCode(
                  preQuestionBeanTool,
                  element.problemId,
                  para["firstOptionCode"]);
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 4:
          widgets = InputWidget4And13(
            title: title,
            text: tempMap[problemId],
            type: 4,
            onTextChange: (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val;
              para["optionValue"] = val;
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 5: //单选和输入大
          if (tempMap[problemId] == null) {
            tempMap[problemId] = <int, dynamic>{};
          }
          widgets = SingleSelectAndInputWidget5(
            title,
            tempMap[problemId][1],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId][1] = val;

              para["firstOptionCode"] = val ?? "";
              para["optionValue"] = getOptionValueByOptionCode(
                  preQuestionBeanTool,
                  element.problemId,
                  para["firstOptionCode"]);
              setNextButtonCanClick();
            },
            element.remark,
            tempMap[problemId][2],
            (val) {
              tempMap[problemId][2] = val;
              para["firstOptionCode"] = val;
              para["optionValue"] = val;
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 6:
          if (tempMap[problemId] == null) {
            tempMap[problemId] = <int, dynamic>{};
          }
          setParas(List<String> select) {
            if (p6["input"] == null || p6["input"] == "") {
              para["firstOptionCode"] = p6["select"];
              para["optionValue"] = select
                  .map((e) {
                    return getOptionValueByOptionCode(
                            preQuestionBeanTool, element.problemId, e) ??
                        "";
                  })
                  .toList()
                  .toParaString();
            } else {
              if (p6["select"] == null || p6["select"] == "") {
                para["firstOptionCode"] = p6["input"];
                para["optionValue"] = p6["input"];
              } else {
                para["firstOptionCode"] = p6["select"] + "," + p6["input"];
                para["optionValue"] = select
                        .map((e) {
                          return getOptionValueByOptionCode(
                                  preQuestionBeanTool, element.problemId, e) ??
                              "";
                        })
                        .toList()
                        .toParaString() +
                    "," +
                    p6["input"];
              }
            }
          }
          widgets = MultipleSelectAndInputWidget6(
            title,
            tempMap[problemId][1] ?? [],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId][1] = val;
              p6["select"] = val.toParaString();
              setParas(val);
              setNextButtonCanClick();
            },
            element.remark,
            tempMap[problemId][2],
            (select, val) {
              tempMap[problemId][2] = val;
              p6["input"] = val;
              setParas(select);
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 7:
          widgets = AreaSelectWidget7(
            title,
            tempMap[problemId],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val;
              para["optionValue"] = getOptionValueByOptionCode(
                  preQuestionBeanTool,
                  element.problemId,
                  para["firstOptionCode"]);
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 8:
          var initDate = tempMap[problemId] ?? DateTime.now();
          para["firstOptionCode"] = DateFormat("y-M-d").format(initDate);
          para["optionValue"] = DateFormat("y-M-d").format(initDate);
          widgets = DatePickWidget8(
            title,
            initDate,
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = DateFormat("y-M-d").format(val);
              para["optionValue"] = DateFormat("y-M-d").format(val);
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 9:
          if (tempMap[problemId] == null) {
            tempMap[problemId] = <int, dynamic>{};
          }
          setParas(val, list) {
            tempMap[problemId][1] = val;
            tempMap[problemId][2] = list;
            para["firstOptionCode"] = val;
            para["optionValue"] = val;
          }
          widgets = LocationSelectWidget9(
            title,
            tempMap[problemId][1],
            tempMap[problemId][2],
            (val, list) {
              setParas(val, list);
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 10:
          if (tempMap[problemId] == null) {
            tempMap[problemId] = <int, dynamic>{};
          }
          var now = DateTime.now();
          var startT = now.add(const Duration(hours: 1));
          var endT = now.add(const Duration(hours: 3));
          var initBegin = tempMap[problemId][1] ??
              TimeOfDay(hour: startT.hour, minute: 0);
          var initEnd = tempMap[problemId][2] ??
              TimeOfDay(hour: endT.hour, minute: 0);
          String str1 =
              initBegin.toString().split("(").last.replaceAll(")", "");
          String str2 = initEnd.toString().split("(").last.replaceAll(")", "");
          para["firstOptionCode"] = "$str1-$str2";
          para["optionValue"] = para["firstOptionCode"];
          //
          widgets = TimePickWidget10(
            title,
            initBegin,
            initEnd,
            (start, end) {
              tempMap[problemId][1] = start;
              tempMap[problemId][2] = end;
              String str1 =
                  start.toString().split("(").last.replaceAll(")", "");
              String str2 = end.toString().split("(").last.replaceAll(")", "");
              para["firstOptionCode"] = "$str1-$str2";
              para["optionValue"] = para["firstOptionCode"];
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 11:
          widgets = ImagePickerWidget11And14(
            title,
            tempMap[problemId] ?? [],
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val.toParaString();
              para["optionValue"] = val.toParaString();
              setNextButtonCanClick();
            },
            type: 11,
            request: request,
          );
          break;
        case 12:
          if (tempMap[problemId] == null) {
            tempMap[problemId] = <int, dynamic>{};
          }
          widgets = SingleSelectAndInputWidget12(
            title,
            tempMap[problemId][1],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId][1] = val;
              para["firstOptionCode"] = val ?? "";
              para["optionValue"] = getOptionValueByOptionCode(
                  preQuestionBeanTool,
                  element.problemId,
                  para["firstOptionCode"]);
              setNextButtonCanClick();
            },
            element.remark,
            tempMap[problemId][2],
            (val) {
              tempMap[problemId][2] = val;
              para["firstOptionCode"] = val ?? "";
              para["optionValue"] = val ?? "";
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 13:
          widgets = InputWidget4And13(
            title: title,
            text: tempMap[problemId],
            type: 13,
            placeholder: element.remark,
            onTextChange: (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val;
              para["optionValue"] = val;
              setNextButtonCanClick();
            },
            request: request,
          );
          break;
        case 14:
          widgets = ImagePickerWidget11And14(
            title,
            tempMap[problemId] ?? [],
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val.toParaString();
              para["optionValue"] = val.toParaString();
              setNextButtonCanClick();
            },
            type: 14,
            request: request,
          );
          break;
        case 15:
          widgets = SingleSelectWidget1(
            title,
            tempMap[problemId],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val ?? "";
              para["optionValue"] = getOptionValueByOptionCode(
                  preQuestionBeanTool,
                  element.problemId,
                  para["firstOptionCode"]);
              setNextButtonCanClick();
            },
            type: 15,
            request: request,
          );
          break;
        case 16:
          if (tempMap[problemId] == null) {
            tempMap[problemId] = <int, dynamic>{};
          }
          widgets = SelectItemWidget16(
            title,
            tempMap[problemId][1],
            element.getKeyValueArrayByList,
            (val1, val2) {
              tempMap[problemId][1] = val1;
              tempMap[problemId][2] = val2;
              if (val2) {
                para["firstOptionCode"] = "否";
                para["optionValue"] = "否";
              } else {
                para["firstOptionCode"] = val1 ?? "";
                para["optionValue"] = getOptionValueByOptionCode(
                    preQuestionBeanTool,
                    element.problemId,
                    para["firstOptionCode"]);
              }
              setNextButtonCanClick();
            },
            tempMap[problemId][2] ?? false,
            request: request,
          );
          break;
        case 17:
          widgets = SingleSelectWidget1(
            title,
            tempMap[problemId],
            element.getKeyValueArrayByList,
            (val) {
              tempMap[problemId] = val;
              para["firstOptionCode"] = val ?? "";
              para["optionValue"] = getOptionValueByOptionCode(
                  preQuestionBeanTool,
                  element.problemId,
                  para["firstOptionCode"]);
              setNextButtonCanClick();
            },
            type: 17,
            remark: element.remark,
            request: request,
          );
          break;
      }
      subWidget.add(widgets.setContainer(margin: EdgeInsets.only(top: 28.w)));
    });
  }
  setNextButtonCanClick();
  subWidget.add(SizedBox(height: 120.w)); //占位
  return subWidget;
}

String? getOptionValueByOptionCode(PreQuestionBeanTool preQuestionBeanTool,
    int? problemId, String? optionCode) {
  if (optionCode == null || optionCode.isEmpty) return "";
  PreQuestionBean? preQuestionBean =
      preQuestionBeanTool?.rows?.firstWhere((model) {
    return model.problemId == problemId;
  });

  return preQuestionBean!.list!.firstWhere((element) {
    return element.optionCode.toString() == optionCode;
  }).optionValue;
}
